package com.newlink.building.common_base.utils

import android.content.Context
import android.util.TypedValue


/**
 * Created by xiaodong on 2022/8/30.
 */
object Base_DensityUtil {

    @JvmStatic
    fun dp2px(context: Context, dpValue: Int): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    @JvmStatic
    fun px2dp(context: Context, dpValue: Int): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue / scale + 0.5f).toInt()
    }

    @JvmStatic
    fun sp2px(context: Context, spVal: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            spVal, context.resources.displayMetrics
        ).toInt()
    }

    @JvmStatic
    fun px2sp(context: Context, pxVal: Float): Float {
        return pxVal / context.resources.displayMetrics.scaledDensity
    }

}